package cn.home.service.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.home.common.annotation.Log;
import cn.home.common.core.controller.BaseController;
import cn.home.common.core.domain.AjaxResult;
import cn.home.common.enums.BusinessType;
import cn.home.service.domain.HomeServiceRecord;
import cn.home.service.service.IHomeServiceRecordService;
import cn.home.common.utils.poi.ExcelUtil;
import cn.home.common.core.page.TableDataInfo;

/**
 * 服务记录Controller
 * 
 * @author xsc
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/service_record/service_record")
public class HomeServiceRecordController extends BaseController
{
    @Autowired
    private IHomeServiceRecordService homeServiceRecordService;

    /**
     * 查询服务记录列表
     */
    @PreAuthorize("@ss.hasPermi('service_record:service_record:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomeServiceRecord homeServiceRecord)
    {
        startPage();
        List<HomeServiceRecord> list = homeServiceRecordService.selectHomeServiceRecordList(homeServiceRecord);
        return getDataTable(list);
    }

    /**
     * 导出服务记录列表
     */
    @PreAuthorize("@ss.hasPermi('service_record:service_record:export')")
    @Log(title = "服务记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomeServiceRecord homeServiceRecord)
    {
        List<HomeServiceRecord> list = homeServiceRecordService.selectHomeServiceRecordList(homeServiceRecord);
        ExcelUtil<HomeServiceRecord> util = new ExcelUtil<HomeServiceRecord>(HomeServiceRecord.class);
        util.exportExcel(response, list, "服务记录数据");
    }

    /**
     * 获取服务记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('service_record:service_record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(homeServiceRecordService.selectHomeServiceRecordById(id));
    }

    /**
     * 新增服务记录
     */
    @PreAuthorize("@ss.hasPermi('service_record:service_record:add')")
    @Log(title = "服务记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomeServiceRecord homeServiceRecord)
    {
        return toAjax(homeServiceRecordService.insertHomeServiceRecord(homeServiceRecord));
    }

    /**
     * 修改服务记录
     */
    @PreAuthorize("@ss.hasPermi('service_record:service_record:edit')")
    @Log(title = "服务记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomeServiceRecord homeServiceRecord)
    {
        return toAjax(homeServiceRecordService.updateHomeServiceRecord(homeServiceRecord));
    }

    /**
     * 删除服务记录
     */
    @PreAuthorize("@ss.hasPermi('service_record:service_record:remove')")
    @Log(title = "服务记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(homeServiceRecordService.deleteHomeServiceRecordByIds(ids));
    }
    /*根据用户id查看服务记录*/
    @PreAuthorize("@ss.hasPermi('service_record:service_record:query')")
    @GetMapping("/user/{userId}")
    public AjaxResult getServiceRecordByUserId(@PathVariable Long userId)
    {
        return success(homeServiceRecordService.selectHomeServiceRecordByUserId(userId));
    }
}
